package com.vimi8.ebb.auth.configuration;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

import java.util.Map;
import java.util.Set;

/**
 * Created by xuybin on 2016/8/5.
 */
public class OAuth2AuthenticationImpl {
    public static String loginName(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        return  oAuth2Authentication.getName();
    }

    public static String openId(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        final Map<String, String> details=(Map<String, String>)((OAuth2AuthenticationDetails)oAuth2Authentication.getDetails()).getDecodedDetails();
        return details.get(BaseJwtAccessTokenConverterImpl.OPENID);
    }

    public static String job(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        final Map<String, String> details=(Map<String, String>)((OAuth2AuthenticationDetails)oAuth2Authentication.getDetails()).getDecodedDetails();
        return details.get(BaseJwtAccessTokenConverterImpl.JOB);
    }
    public static String ClientOrgClass(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        final Map<String, String> details=(Map<String, String>)((OAuth2AuthenticationDetails)oAuth2Authentication.getDetails()).getDecodedDetails();
        return details.get(BaseJwtAccessTokenConverterImpl.CLIENTORGCLASS);
    }

    public static String tokenValue(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        final OAuth2AuthenticationDetails oAuth2AuthenticationDetails=(OAuth2AuthenticationDetails)oAuth2Authentication.getDetails();
        return oAuth2AuthenticationDetails.getTokenValue();
    }

    public static String clientId(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        return oAuth2Authentication.getOAuth2Request().getClientId();
    }

    public static Set<String> scope(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        return oAuth2Authentication.getOAuth2Request().getScope();
    }

    public static String redirectUri(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        return oAuth2Authentication.getOAuth2Request().getRedirectUri();
    }

    public static Long expiresIn(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        final Map<String, String> details=(Map<String, String>)((OAuth2AuthenticationDetails)oAuth2Authentication.getDetails()).getDecodedDetails();
        return Long.valueOf(details.get(BaseJwtAccessTokenConverterImpl.EXPIRES_IN));
    }
    public static String jti(){
        OAuth2Authentication oAuth2Authentication = (OAuth2Authentication)SecurityContextHolder.getContext().getAuthentication();
        final Map<String, String> details=(Map<String, String>)((OAuth2AuthenticationDetails)oAuth2Authentication.getDetails()).getDecodedDetails();
        return details.get(BaseJwtAccessTokenConverterImpl.JTI);
    }
}
